package edu.unc.comp410.colemangeer.model.constraints;


public class ZipCodeStringConstraint extends StringConstraint {
	
	private String stringVal;

	public ZipCodeStringConstraint( String theName, boolean theOptional ) {
		super( theName, theOptional);
	}

	@Override
	public boolean isLegal(Object theObj){
		
		boolean legalResult = true;
		
		try{
			stringVal = (String) theObj;
			legalResult = super.isLegal(stringVal) && isLegalZipString(stringVal);
		}
		catch (ClassCastException e){
			legalResult = false;
		}
		
		return legalResult;
	}
	
	private boolean isLegalZipString(String zip){
		
		if (zip.length() == 5){
			for (Character c: zip.toCharArray()){
				if (!(Character.isDigit(c))){
					return false;
				}
			}
		}
		else{
			// length was not 5
			return false;
		}
		return true;
	}
}